import axios from 'axios'

const instance = axios.create({
  baseURL: '',
  withCredentials: false,
  timeout: 60000,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
  }
})

// request intercept 
instance.interceptors.request.use(
  config => {
    return config
  },
  error => Promise.error(error))

// response intercept
instance.interceptors.response.use(
  res => {
    if (res.status === 200) {
      const responseData = res.data
      if (responseData.size || !responseData.flag) {
        return Promise.resolve(res.data)
      } else {
        if (responseData.flag == '200') {
          return Promise.resolve(res.data.res)
        } else {
          return Promise.reject(res.data)
        }
      }
    } else {
      return Promise.reject(res)
    }
  },
  error => {
    const { response } = error
    if (response) {
      console.log(response)
      return Promise.reject(response)
    } else {
      if (!window.navigator.onLine) {
        // store.commit('changeNetwork', false)
      } else {
        return Promise.reject(error)
      }
    }
  })

export default instance